package biz.impl;

import java.util.List;
import java.util.Map;

import biz.TeamManagerBiz;
import dao.TeamManagerDao;
import pojo.PageTeams;
import pojo.Projects;
import pojo.Teams;
import pojo.UserRoleProject;
import pojo.UserTeam;
import pojo.Users;



public class TeamManagerBizImpl implements TeamManagerBiz{

	private TeamManagerDao teamManagerDao;
	
	public TeamManagerDao getTeamManagerDao() {
		return teamManagerDao;
	}

	public void setTeamManagerDao(TeamManagerDao teamManagerDao) {
		this.teamManagerDao = teamManagerDao;
	}
	
	
	@Override
	public List<Map<String,String>> getApplyUsers1ByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getApplyUsers1ByTeamid(teamid);
	}

	@Override
	public List<Map<String,String>> getApplyUsers2ByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getApplyUsers2ByTeamid(teamid);
	}



	@Override
	public Users getUserByUserid(int userid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getUserByUserid(userid);
	}

	@Override
	public int updateUserTeamIsAccepted(int userid,int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.updateUserTeamIsAccepted(userid,teamid);
	}

	@Override
	public int getApplyTotalNo(int teamid) {
			int applyTotalNo=0;
			List<Map<String,String>> list=getApplyUsers2ByTeamid(teamid);
			if(list!=null){
				int length=list.size();
			if(length%4==0){
				applyTotalNo=length/4;
			}else{
				applyTotalNo=length/4+1;
			}
			}
			return applyTotalNo;
	}

	@Override
	public List<Map<String,String>> appluUserFenYePrev(int applyNowNo, int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.appluUserFenYePrev(applyNowNo, teamid);
	}

	@Override
	public List<Map<String,String>> appluUserFenYeNext(int applyNowNo, int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.appluUserFenYeNext(applyNowNo, teamid);
	}

	@Override
	public List<Map<String, String>> getPartApplyUsersByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getPartApplyUsersByTeamid(teamid);
	}

	@Override
	public List<Map<String, String>> getPart2ApplyUsersByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getPart2ApplyUsersByTeamid(teamid);
	}

	@Override
	public List<Map<String, String>> getCreateUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getCreateUserByTeamid(teamid);
	}

	@Override
	public List<Map<String, String>> getAllTeamByUserid(int userid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getAllTeamByUserid(userid);
	}

	@Override
	public List<Users> getAllTeamUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getAllTeamUserByTeamid(teamid);
	}

	@Override
	public List<Users> getPartTeamUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getPartTeamUserByTeamid(teamid);
	}

	@Override
	public int getTeamUserTotalNo(int teamid) {
		int applyTotalNo=0;
		List<Users> list=getAllTeamUserByTeamid(teamid);
		if(list!=null){
			int length=list.size();
		if(length%4==0){
			applyTotalNo=length/4;
		}else{
			applyTotalNo=length/4+1;
		}
		}
		return applyTotalNo;
}

	@Override
	public List<Users> teamUserFenYe(int teamUserNowNo, int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.teamUserFenYe(teamUserNowNo, teamid);
	}

	@Override
	public int deleteTeamMember(int userid, int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.deleteTeamMember(userid, teamid);
	}

	@Override
	public int addProject(Projects p) {
		// TODO Auto-generated method stub
		return teamManagerDao.addProject(p);
	}

	@Override
	public int addProjectMember(UserRoleProject urp) {
		// TODO Auto-generated method stub
		return teamManagerDao.addProjectMember(urp);
	}



	@Override
	public int addProjectAndMember(Projects p, int managerId, String[] s) {
		// TODO Auto-generated method stub
		int c=0;
		int a=addProject(p);
	
		if(a>0){
		  int b=0;
		  int projectid=p.getProjectId();
		  if(managerId!=0){
		  UserRoleProject managerUrp=new UserRoleProject();
		  managerUrp.setRoleId(2);
		  managerUrp.setProjectId(projectid);
		  managerUrp.setUserId(managerId);
		  b=addProjectMember(managerUrp);
		  }
		  if(b>0){
			 
			  for (int i = 0; i < s.length; i++) {
				int  si=Integer.parseInt(s[i]);
				if(si!=0){
					UserRoleProject memberUrp=new UserRoleProject();
					memberUrp.setRoleId(3);
					memberUrp.setProjectId(projectid);
					memberUrp.setUserId(si);
					c=addProjectMember(memberUrp);
				}
			}
			
	
		  }
			
		}
		return c;
	}

	@Override
	public List<Projects> getAllProjectsByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getAllProjectsByTeamid(teamid);
	}

	@Override
	public List<Map<String, String>> getProjectMember(int projectid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getProjectMember(projectid);
	}

	

	@Override
	public int deleteProjectMember(int urpid) {
		// TODO Auto-generated method stub
		return teamManagerDao.deleteProjectMember(urpid);
	}

	@Override
	public int updateProjectMemberMTP(int projectid, int roleid, int urpid) {
		// TODO Auto-generated method stub
		
		return teamManagerDao.updateProjectMemberMTP(projectid, roleid, urpid);
	}

	@Override
	public int updateProjectMemberPTM(int projectid, int roleid, int urpid,int type) {
		// TODO Auto-generated method stub
		int c=0;
		if(type==0){
		int a= teamManagerDao.updatePrevPTM(projectid, roleid, urpid);
		
		if(a>0){
		c=teamManagerDao.updateProjectMemberPTM(projectid, roleid, urpid);
		}
		}else{
		c=teamManagerDao.updateProjectMemberPTM(projectid, roleid, urpid);
		}
		return c;
	}


	//XUYnjin

	@Override
	public List<Map<String,Object>> getTeamByName(String teamName) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.getTeamByName(teamName);
	}
	
	@Override
	public PageTeams getTeams(int currentPage, int pageSize) throws Exception {
		// TODO Auto-generated method stub
//		int count=teamManagerDao.count();
		System.out.println("sec"+currentPage);
		System.out.println("sec"+pageSize);
	    List<Map<String, Object>> list=teamManagerDao.getTeam();
		int count=list.size();
		int totalPage=(int)Math.ceil(count*1.0/pageSize);
		List<Map<String, Object>> teampa=teamManagerDao.getTeams(currentPage,pageSize);
		PageTeams pageteams=new PageTeams();
		pageteams.setCount(count);
		pageteams.setCurrentPage(currentPage);
		pageteams.setPageSize(pageSize);
		pageteams.setTeampa(teampa);
		pageteams.setTotalPage(totalPage);
		return pageteams;
	}


	
	@Override
	public List<Map<String, Object>> gotoTeamList(int userId) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.gotoTeamList(userId);
	}
	
	@Override
	public int addTeam(Teams teams) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.addTeam(teams);
	}
	
	@Override
	public Teams selectTeam(String teamName) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.selectTeam(teamName);
	}
	
	@Override
	public int addUserTeam(UserTeam userteam) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.addUserTeam(userteam);
	}
	
	@Override
	public int updateUsers(Users users) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.updateUsers(users);
	}

	@Override
	public int updateUserp(Users users) throws Exception {
		// TODO Auto-generated method stub
		return teamManagerDao.updateUserp(users);
	}

	@Override
	public Projects getProjectByProjectid(int projectid) {
		// TODO Auto-generated method stub
		return teamManagerDao.getProjectByProjectid(projectid);
	}

	@Override
	public int deleteProjectByProjectid(int projectid) {
		// TODO Auto-generated method stub
		return teamManagerDao.deleteProjectByProjectid(projectid);
	}

	

	@Override
	public int updateProjectInfoByProjectid(Projects p) {
		// TODO Auto-generated method stub
		return teamManagerDao.updateProjectInfoByProjectid(p);
	}
	@Override
	public int addApplyTeam(UserTeam ut) {
		// TODO Auto-generated method stub
		return teamManagerDao.addApplyTeam(ut);
	}

	@Override
	public UserTeam checkIsApplied(int teamid, int userid) {
		// TODO Auto-generated method stub
		return teamManagerDao.checkIsApplied(teamid, userid);
	}
	

	


//	@Override
//	public PageTeams getTeamsec(String teamName, int currentPage, int pageSize) throws Exception {
//		// TODO Auto-generated method stub
//		System.out.println("sec"+currentPage);
//		System.out.println("sec"+pageSize);
//	    List<Map<String, Object>> list=teamManagerDao.getTeamsecc(teamName);
//		int count=list.size();
//		int totalPage=(int)Math.ceil(count*1.0/pageSize);
//		List<Map<String, Object>> teampa=teamManagerDao.getTeamsec(teamName,currentPage,pageSize);
//		PageTeams pageteams=new PageTeams();
//		pageteams.setCount(count);
//		pageteams.setCurrentPage(currentPage);
//		pageteams.setPageSize(pageSize);
//		pageteams.setTeampa(teampa);
//		pageteams.setTotalPage(totalPage);
//		return pageteams;
//	}


	
	


	

	

	

	

	
	

	
	
}
